package com.ming.springbillmanager.contoller;

import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;

import javax.servlet.http.HttpSession;
import java.util.Map;

/**
 * @project_name: spring2
 * @description:
 * @author: arvin w ming
 * @create: 2022-09-08 09:04
 * //"login"
 */

@Controller
public class LoginController {

    //@RequstMapping(value="/login",method=RequestMethod.post)
    @PostMapping("/login")
    public String login(HttpSession session, String username, String password, Map<String,Object> map){
        if(!StringUtils.isEmpty(username) && "111".equals(password)){

            //return "main/index";   //注意,这里是请求转发，return后面的是页面名称

            session.setAttribute("loginUser",username); //  User user = getAttribute("loginUser");  if(user != null) {xxxxx} else {xxxxx}

            return "redirect:/main.html"; //注意,修改成redirect重定向以后，return后面的就不在是页面名称了，而是路径。
        }

        map.put("message","username or password incorrect!!");

        return "main/login";
    }


    //退出逻辑
    @GetMapping("/logout")
    public String logout(HttpSession session){
        //1,调用控制器退出
        //2,清空会话信息(session当中的信息)
        session.removeAttribute("loginUser");
        //3,注销session
        session.invalidate();
        //4,跳转到登录页面 (请求转发? or 重定向?====重定向)
        return "redirect:/index.html";
    }
}
